package coder.user.service.impl;

import coder.common.BaseContext;
import coder.exception.BaseException;
import coder.user.entity.Order;
import coder.user.mapper.OrderMapper;
import coder.user.service.IOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Override
    public void updateOrder(Order order) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getId, order.getId()).eq(Order::getUserId, BaseContext.getCurrentId()).eq(Order::getIsDelete, 0);
        Order result = this.getOne(queryWrapper);
        if(result == null) {
            throw new BaseException("订单不存在");
        }
        this.updateById(order);
    }

    @Override
    public List<Order> queryOrder() {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getUserId, BaseContext.getCurrentId()).eq(Order::getIsDelete, 0);
        return this.list(queryWrapper);
    }

    @Override
    public Order queryOne(String id) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getId, id).eq(Order::getUserId, BaseContext.getCurrentId()).eq(Order::getIsDelete, 0);
        Order order = this.getOne(queryWrapper);
        if(order == null) {
            throw new BaseException("订单不存在");
        }
        return order;
    }
}
